define(
		[ "PDAppDir/properties", "PDUtilDir/util", "PDUtilDir/tool",
				"PDUtilDir/slidebar", "PDUtilDir/inputSelect",
				"css!AppPathDir/product/productInfos/views/css/css" ],
		function(Properties, Util, Tool, Slidebar, inputSelect) {
			function updateSupportInit($scope, $compile) {
				return new updateSupport($scope, $compile);
			}
			function updateSupport($scope, $compile) {
				this.$scope = $scope;
				this.$compile = $compile;
			}
			updateSupport.fn = updateSupport.prototype;
			updateSupport.fn.init = function() {
				this.entity = {
					line: undefined,
					spots : new Array()
				};
				this.bind = {
					spotIds : new Array(),
					spots : new Array()
				};
			};
			updateSupport.fn.digest = function() {
				this.$scope.$digest();
			};
			updateSupport.fn.run = function(data) {
				var _this = this;
				_this.init();
				_this.entity.line = data;
				this.slidebars = Slidebar({
					size : 'lg',
					url : getServer() + "/static/app/product/lineInfos/views/update.html",
//					close : true,
					$scope : this.$scope,
					$compile : this.$compile,
					afterLoad : function() {
						_this.initialization();
					}
				});
			};
			updateSupport.fn.initialization = function() {
				this.baseInit();
				this.spotsInit();
			};

			updateSupport.fn.baseInit = function() {
				var _this = this;
				
				inputSelect.dropCache();
				inputSelect({
					id : "typeName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.types,
					initData: [_this.entity.line.type],
					simpleData : true,
					searchAble : false,
					callback : function(id, data) {
						_this.entity.line.type = id[0];
						_this.$scope.updateLineForm.type.$setViewValue(_this.entity.line.type);
					}
				});
				$("#spotsLineInfo .generate_line .spots .list").mCustomScrollbar({
					axis : "y",
					theme: "minimal-dark"
				});
			};
			updateSupport.fn.spotsInit = function() {
				var _this = this;
				
				//查询当前线路景点列表
				$.ajax({
					url : getServer() + "/flea/hclv/spot/selectByLineId.htmls",
					data : {
						'lineId' : _this.entity.line.id
					},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						var spots = data.data, len = spots.length;
						for (var i = 0; i < len; i++) {
							spots[i].path = Properties.URL_FILE_SERVER_URL + spots[i].path;
							_this.entity.spots.push(spots[i]);
							_this.bind.spotIds.push(spots[i].id);
						}
						// 查询所有景点
						$.ajax({
							url : getServer() + "/flea/hclv/spot/selectMySpot.htmls",
							success : function(data) {
                                if (data.status != 200) {
                                    return;
                                }
								var spotIds = _this.bind.spotIds, index = 0;
								spots = data.data;
								len = spots.length;
								
								for (var i = 0; i < len; i++) {
									index = spotIds.indexOf(spots[i].id);
									if(index < 0){
										spots[i].path = Properties.URL_FILE_SERVER_URL + spots[i].path;
										_this.bind.spots.push(spots[i]);
									}
								}
								_this.digest();
							}
						});
						_this.digest();
					}
				});
			};

			updateSupport.fn.choiceFun = function(id) {
				var ids = this.bind.spotIds, index = ids.indexOf(id), i = 0, spots = undefined, len = 0;

				if (index >= 0) {
					spots = this.entity.spots;
					len = spots.length;

					for (i; i < len; i++) {
						if (id == spots[i].id) {
							spots.splice(i, 1);
							ids.splice(index, 1);
							return;
						}
					}
				}

				spots = this.bind.spots;
				len = spots.length;
				for (i; i < len; i++) {
					if (id == spots[i].id) {
						this.entity.spots.push(spots[i]);
						this.bind.spotIds.push(id);
						spots.splice(i, 1);
						return;
					}
				}
			};
			updateSupport.fn.unChoiceFun = function(id) {
				var spots = this.entity.spots, len = spots.length;
				
				for (var i = 0; i < len; i++) {
					if (id == spots[i].id) {
						this.bind.spots.push($.extend({}, spots[i]))
						spots.splice(i, 1);
						this.bind.spotIds.splice(i, 1);
						return;
					}
				}
			};

			updateSupport.fn.save = function() {
				var _this = this;
//				if ( _this.entity.spots.length == 0) {
//					Util.alert('请选择至少一个景点！');
//				}
				$.ajax({
					url : getServer() + "/flea/hclv/line/update.htmls",
					data : {
						'model' : JSON.stringify(_this.entity)
					},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						_this.$scope.grid.refresh();
						Util.alert(data.message);
						_this.slidebars.close();
					}
				});
			};
			return updateSupportInit;
		});